/**
 * Copyright © 2021 郑心怡. All rights reserved.
 *
 * @Title:UserRoleServiceImpl.java
 * @Prject: com.interesting.examsystem.user.service.impl
 * @Package: com.interesting.examsystem.user.service.impl
 * @author: ZhengXinyi
 * @date: 2021-07-24
 * @version: V1.0
 */
package com.interesting.examsystem.user.service.impl;


import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import com.interesting.examsystem.role.model.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.interesting.examsystem.common.utils.BeanMapperUtils;
import com.interesting.examsystem.user.model.UserRoleDO;
import com.interesting.examsystem.user.mapper.UserRoleMapper;
import com.interesting.examsystem.user.service.IUserRoleService;
import com.interesting.examsystem.user.model.UserRoleDTO;
import com.interesting.examsystem.user.model.UserRoleVO;

/**
 * @ClassName: UserRoleServiceImpl
 * @Description: 服务接口实现
 * @author: ZhengXinyi
 * @date: 2021-07-24
 */
@Slf4j
@Transactional
@Service("userRoleService")
public class UserRoleServiceImpl implements IUserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public Page<UserRoleVO> selectPageVo(Page<UserRoleDTO> page, UserRoleDTO userRoleDto) {
        OrderItem orderItem;
        if (!StrUtil.isEmpty(userRoleDto.getSortMapJson())) {
            JSONObject jsonObject = JSONUtil.parseObj(userRoleDto.getSortMapJson());
            for (Entry<String, Object> orderEntry : jsonObject.entrySet()) {
                orderItem = new OrderItem();
                orderItem.setAsc((boolean) orderEntry.getValue());
                orderItem.setColumn(orderEntry.getKey());
                page.addOrder(orderItem);
            }
        } else {
            orderItem = new OrderItem();
            orderItem.setAsc(true);
            orderItem.setColumn("SORT_NUMBER");
            page.addOrder(orderItem);
        }
        Page<UserRoleVO> resultPage = userRoleMapper.selectPageVo(page, userRoleDto);
        return resultPage;
    }

    @Override
    public UserRoleVO selectById(String id) {
        UserRoleVO userRoleVo = null;
        UserRoleDO userRole = (UserRoleDO) userRoleMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(userRole)) {
            userRoleVo = BeanMapperUtils.map(userRole, UserRoleVO.class);
        }
        return userRoleVo;
    }

    @Override
    public UserRoleVO selectOne(UserRoleDTO userRoleDto) {
        UserRoleVO userRoleVo = null;
        UserRoleDO entity = BeanMapperUtils.map(userRoleDto, UserRoleDO.class);
        QueryWrapper<UserRoleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(entity);
        UserRoleDO userRoleDo = userRoleMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(userRoleDo)) {
            userRoleVo = BeanMapperUtils.map(userRoleDo, UserRoleVO.class);
        }
        return userRoleVo;
    }

    @Override
    public int deleteBatchIds(List<String> ids) {
        return userRoleMapper.deleteBatchIds(ids);
    }

    @Override
    public int insert(UserRoleVO userRoleVo) {
        userRoleVo.setId(IdUtil.objectId());
        UserRoleDO entity = BeanMapperUtils.map(userRoleVo, UserRoleDO.class);
        return userRoleMapper.insert(entity);
    }

    @Override
    public int update(UserRoleVO userRoleVo) {
        UpdateWrapper<UserRoleDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", userRoleVo.getId());
        UserRoleDO entity = BeanMapperUtils.map(userRoleVo, UserRoleDO.class);
        return userRoleMapper.update(entity, updateWrapper);
    }

    @Override
    public void insertBatch(List<UserRoleVO> userRoleVos) {
        if (!ObjectUtil.isEmpty(userRoleVos)) {
            userRoleMapper.insertBatch(userRoleVos);
        }
    }

    /**
     * 根据用户获取角色表
     *
     * @param id
     * @return
     */
    @Override
    public List<RoleVO> selectListByUser(String id) {
        return userRoleMapper.selectListByUser(id);
    }
}
